import java.util.HashMap;

/*
剑指 Offer 48. 最长不含重复字符的子字符串
解题思路：双指针+哈希表，右指针先移动，当移动到有相同字母时，更新左指针，此时左右指针的距离就是字幕长度
 */
public class Offer48 {
  public static void main(String[] args) {
    System.out.println(new Offer48().lengthOfLongestSubstring("abcadbf"));
  }

  public int lengthOfLongestSubstring(String s){
    int res = 0;
    int i = -1;
    HashMap<Character, Integer> map = new HashMap<>();
    for (int j = 0; j < s.length(); j++) {
      if (map.containsKey(s.charAt(j))){
        i = Math.max(i,map.get(s.charAt(j)));
      }
      map.put(s.charAt(j),j);
      res = Math.max(res,j - i);
    }
    return res;
  }
}
